package com.hse.gui;

import com.hse.SensorAnalyser;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Nosov Alexander
 * Date: 22.05.2009
 * Time: 17:07:14
 */
public class Form {
    public JPanel panel1;
    private JList list1;
    private JTextArea logField;
    private JScrollPane logScrollPane;
    private JPanel mainAreaPanel;
    private DefaultListModel listModel;

    private ArrayList<ClientComponentBorder> clients = new ArrayList<ClientComponentBorder>();

    SensorAnalyser sensorAnalyser = new SensorAnalyser(this);


    public Form() {
        sensorAnalyser.start();

        listModel = (DefaultListModel) list1.getModel();

        //Test data

        log("Welcome to Sensor Analyser 3000...");


        list1.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int[] indices = list1.getSelectedIndices();
                for (int i = 0; i < clients.size(); i++) {
                    for (int j = 0; j < indices.length; j++) {
                        if (i == indices[j]) {
                            clients.get(i).setVisible(true);
                            break;
                        } else {
                            clients.get(i).setVisible(false);
                        }

                    }
                }
                mainAreaPanel.repaint();
                /*
                Object[] values=list1.getSelectedValues();
                StringBuilder str=new StringBuilder("Показать данные из ");
                for (Object value : values) {
                    String word = (String) value;
                    str.append("\"" + word + "\" ");

                }
                informationboutSensors.setText(str.toString());
                log(str.toString());
                */
            }
        });
    }


    /*
    * API
    */

    /*
     * Log something
     */

    public void log(String str) {
        log(str, new Date());
    }

    /*
     * Log something with specific time
     */
    public void log(String str, Date date) {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        logField.insert(dateFormat.format(date) + ": " + str + "\n", 0);
    }

    public void addClient(IClientComponent client) {
        ClientComponentBorder clientComponentBorder = new ClientComponentBorder(client, client.toString());
        listModel.addElement(clientComponentBorder);

        clients.add(clientComponentBorder);
        mainAreaPanel.add(clientComponentBorder);
        mainAreaPanel.revalidate();

    }

    public void removeClients() {
        for (ClientComponentBorder client : clients) {
            client.setVisible(false);
            client = null;
        }
        listModel.clear();
        mainAreaPanel.revalidate();
    }

    public void showErrorMessage(String str) {
        JOptionPane.showMessageDialog(null, str);
    }


}
